<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="填报人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep v-decorator="['reportPersion', validatorRules.reportPersion]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="填报时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择填报时间" v-decorator="['writeTime', validatorRules.writeTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-item label="最后修改时间" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <j-date placeholder="请选择最后修改时间" v-decorator="['lastWriteTime']" :trigger-change="true" style="width: 100%" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :span='12'>
            <a-form-item label='项目名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-popup
                v-decorator="['projectSortName',validatorRules.projectShortName]"
                :trigger-change='true'
                org-fields='id,short_name'
                dest-fields='projectId,projectSortName'
                code='pm_project_select'
                @callback='popupCallback'
              />
              <input v-decorator="['projectId']" v-show='false'></input>
            </a-form-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-item label="项目简称" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--                          <a-input-->
<!--                            v-decorator="['projectSortName', validatorRules.projectSortName]"-->
<!--                            placeholder="请选择项目简称"-->
<!--                            @click="projectShortNameClick"-->
<!--                          ></a-input>-->
<!--                          <a-input v-decorator="['projectId']" v-show='false'></a-input>-->
<!--                        </a-form-item>-->
<!--          </a-col>-->
          <a-col :span="12">
            <a-form-item label="项目阶段" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['projectPhase']" :trigger-change="true" dictCode="project_phase" placeholder="请选择项目阶段" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="任务数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['taskNum', validatorRules.taskNum]" placeholder="请输入任务数量"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="工时" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['workHours', validatorRules.workHours]" placeholder="请输入工时"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="是否补录" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show='false'>
              <j-dict-select-tag type="list" v-decorator="['isReplenish', validatorRules.isReplenish]" :trigger-change="true" dictCode="yes_no" placeholder="请选择是否补录" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="今日完成" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['todayWork', validatorRules.todayWork]" rows="4" placeholder="请输入今日完成" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="明日计划" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['nextDayWork', validatorRules.nextDayWork]" rows="4" placeholder="请输入明日计划" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="待协调事项" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['waitHelpWork']" rows="4" placeholder="请输入待协调事项" />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  <hf-Project-Select-Modal ref='hfProjectSelectModal' :multiple="false"
                               @hf-pm-project-select="hfPmProjectSelect"></hf-Project-Select-Modal>
    </a-spin>
  </template>

  <script>

    import { httpAction, getAction } from '@/api/manage'
    import pick from 'lodash.pick'
    import { validateDuplicateValue } from '@/utils/util'
    import HfProjectSelectModal from '@views/hifar/pm/modules/HfProjectSelectModal'

    export default {
      name: 'HfPmWorkReportsForm',
      components: {
        HfProjectSelectModal
      },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        confirmLoading: false,
        validatorRules: {
          reportPersion: {
            rules: [
              { required: true, message: '请输入填报人!'},
            ]
          },
          writeTime: {
            rules: [
              { required: true, message: '请输入填报时间!'},
            ]
          },
          projectSortName: {
            rules: [
              { required: true, message: '请输入项目简称!'},
            ]
          },
          taskNum: {
            rules: [
              { required: true, message: '请输入任务数量!'},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
            ]
          },
          workHours: {
            rules: [
              { required: true, message: '请输入工时!'},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
            ]
          },
          isReplenish: {
            initialValue:"yes",
            rules: [
            ]
          },
          todayWork: {
            rules: [
              { required: true, message: '请输入今日完成!'},
            ]
          },
          nextDayWork: {
            rules: [
              { required: true, message: '请输入明日计划!'},
            ]
          },
        },
        url: {
          add: "/pm/hfPmWorkReports/add",
          edit: "/pm/hfPmWorkReports/edit",
          queryById: "/pm/hfPmWorkReports/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'reportPersion','writeTime','lastWriteTime','projectSortName','projectId','projectPhase','taskNum','workHours','isReplenish','todayWork','nextDayWork','waitHelpWork'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      projectShortNameClick() {
              this.$refs.hfProjectSelectModal.title = '选择项目'
              this.$refs.hfProjectSelectModal.visible = true

            },
            hfPmProjectSelect(data) {
              this.form.setFieldsValue({
                projectId: data.id,
                projectSortName: data.shortName
              })
            },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'reportPersion','writeTime','lastWriteTime','projectSortName','projectId','projectPhase','taskNum','workHours','isReplenish','todayWork','nextDayWork','waitHelpWork'))
      },
    }
  }
</script>